package com.luoluo.controller;

import com.luoluo.pojo.LeaveWord;
import com.luoluo.pojo.TouristsLeaveWord;
import com.luoluo.pojo.UserTable;
import com.luoluo.service.impl.LeaveWordServiceImpl;
import com.luoluo.service.impl.TouristsLeaveWordServiceImpl;
import com.luoluo.service.impl.UserTableServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LeaveWordController {

    @Autowired
    LeaveWordServiceImpl leaveWordServiceImpl;
    @Autowired
    TouristsLeaveWordServiceImpl touristsLeaveWordServiceImpl;
    @Autowired
    UserTableServiceImpl userTableServiceImpl;

    /**
     * 查询留言，按条查询
     * @param number
     * @param model
     * @return
     */
    @GetMapping("leaveword")
    public String index(@RequestParam(value = "part",defaultValue = "10")Integer number, Model model){
        model.addAttribute( "leaveWords", leaveWordServiceImpl.selectPart(number));
        return "index";
    }

    /**
     * 查询留言，按id查询，查询用户数据
     * @return
     */
    @GetMapping("leaveword/{id}")
    public String selectById(@PathVariable("id")Integer id, Model model){
        System.out.println("尝试查询全部...");
        LeaveWord leaveWord = leaveWordServiceImpl.selectById(id);
        model.addAttribute("leaveWord", leaveWord);
        System.out.println("留言查询完成..."+leaveWord.getId());

        TouristsLeaveWord touristsLeaveWord = touristsLeaveWordServiceImpl.selectByLeaveWordId(leaveWord.getId());
        model.addAttribute("touristsLeaveWord", touristsLeaveWord);
        System.out.println("游客查询完成..."+touristsLeaveWord);
        UserTable userTable = userTableServiceImpl.selectByLeaveWordId(leaveWord.getId());
        model.addAttribute("userTable", userTable);
        System.out.println("用户查询完成..."+userTable);
        return "select";
    }

    /**
     * 添加留言
     * @param request
     * @return 是否成功的信息
     */
    @ResponseBody
    @PostMapping("leaveword")
    public String create(HttpServletRequest request){
        return leaveWordServiceImpl.touristsLeaveWord(request);
    }

}
